package es.puravida.watchuoc.client;

import java.util.ArrayList;
import java.util.List;

import es.puravida.watchuoc.R;
import es.puravida.watchuoc.WatchUOCApplication;
import es.puravida.watchuoc.R.id;
import es.puravida.watchuoc.R.layout;
import es.puravida.watchuoc.service.WatchUOCService;
import es.puravida.watchuoc.util.MyHTTPClient;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class SituacionActivity extends Activity {

	private static final String TAG = SituacionActivity.class.getName();

	private String session = null;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "starting activity");

		NotificationManager notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationMgr.cancelAll();

		setContentView(R.layout.situacion);

		Bundle bundle = this.getIntent().getExtras();
		String[] aulas = bundle.getStringArray("es.puravida.watchuoc.aulas");
		int hay = 0;
		List<String> tmp = new ArrayList<String>();
		for (String aula : aulas) {
			int hayAula = bundle.getInt("es.puravida.watchuoc.aulas." + aula);
			hay += hayAula;
			tmp.add(aula + " (" + hayAula + ")");
		}
		ListView e = (ListView) this.findViewById(R.id.aulasListView);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.aula_item, tmp);
		e.setAdapter(adapter);

		Button bV = (Button) this.findViewById(R.id.buttonVigilar);
		bV.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				startWatchUOCService();
				endActivity();
			}
		});

		Button bT = (Button) this.findViewById(R.id.buttonTerminar);
		bT.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				stopService();
				endActivity();
			}
		});

	}

	protected void endActivity() {
		this.finish();
	}
	
	protected void stopService(){
		Intent launch = new Intent(SituacionActivity.this,
				WatchUOCService.class);
		stopService(launch);
	}
	
	protected void startWatchUOCService() {
		Intent launch = new Intent(SituacionActivity.this, WatchUOCService.class);
		launch.putExtra("es.puravida.watchuoc.doprocess", false);
		startService(launch);
	}
	
}
